var getId = function(id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};
function ScreenPoint(screenX, screenY) {
	this.screenX = screenX;
	this.screenY = screenY;
	return this;
}
ScreenPoint.prototype.setX = function(screenX) {
	this.screenX = screenX;
};
ScreenPoint.prototype.setY = function(screenY) {
	this.screenY = screenY;
};
ScreenPoint.prototype.toString = function() {
	return this.screenX.toString() + " ---  " + this.screenY.toString();
};
String.prototype.replaceAll = function(s1, s2) {
	return this.replace(new RegExp(s1, "gm"), s2);
}
var coords = new Array();
function initTrackCoords(points) {
	if (points != "") {
		points = points.replaceAll("\\[", "").replaceAll("\\]", "");
		var pointCoord = points.split(";");
		for ( var i = 0; i < pointCoord.length; i++) {
			var readerID = pointCoord[i].substring(0, pointCoord[i]
					.indexOf(":"));
			pointCoord[i] = pointCoord[i]
					.substring(pointCoord[i].indexOf(":") + 1);
			var pintInfo = pointCoord[i].split(",");
			coords[i] = new Array();
			coords[i][0] = pintInfo[0] + "," + pintInfo[1];
			coords[i][1] = readerID;
			if (pintInfo.length > 2) {
				coords[i][2] = pintInfo[2];
			} else {
				coords[i][2] = "";
			}
		}
		darwTrackLine();
	}
}
function darwTrackLine() {
	if (coords.length > 0) {
		var coord = getCoord(0, 0);
		var coordXY = coord.split(",");
		var coordX = coordXY[0];
		var coordY = coordXY[1];
		var startPoint = new ScreenPoint(coordX, coordY);
		var endPoint = new ScreenPoint(coordX, coordY);
		line = drawLine("line" + coordLen, startPoint, endPoint);
		getId("empImg").style.left = coordX + "px";
		getId("empImg").style.top = coordY + "px";
		getId("intimeP").style.left = coordX - 15 + "px";
		getId("intimeP").style.top = coordY - 20 + "px";
		if (getCoord(coordLen, 2) != "") {
			getId("intimeP").style.visibility = "visible";
			getId("intimeP").innerText = getCoord(coordLen, 2);
		}
		tarckLine();
	}
}
function getCoord(len, index) {
	return coords[len][index];
}
var stepNum = 2;
var line;
var interval = 30;
var timeoutProcess;
var toX = 0;
var toY = 0;
var coordLen = 0;
var trackFlag = false;
var stepLen = 0;
var centerFlag = true;
function tarckLine() {
	var speed = interval;
	if (coordLen < coords.length) {
		var coord = getCoord(coordLen, 0);
		var coordXY = coord.split(",");
		var coordX = coordXY[0];
		var coordY = coordXY[1];
		var startPoint = new ScreenPoint(coordX, coordY);
		var endPoint = new ScreenPoint(coordX, coordY);
		if ((coordLen + 1) < coords.length) {
			coordXY = getCoord(coordLen + 1, 0).split(",");
			endPoint = new ScreenPoint(coordXY[0], coordXY[1]);
		} else {
			clearTimeout(timeoutProcess);
			return false;
		}
		if (toX == parseInt(endPoint.screenX)
				&& toY == parseInt(endPoint.screenY)) {
			coordLen++;
			coordXY = getCoord(coordLen, 0).split(",");
			getId("intimeP").style.visibility = "hidden";
			if (getCoord(coordLen, 2) != "") {
				getId("intimeP").style.visibility = "visible";
				getId("intimeP").innerText = getCoord(coordLen, 2);
			}
			startPoint = new ScreenPoint(coordXY[0], coordXY[1]);
			endPoint = new ScreenPoint(coordXY[0], coordXY[1]);
			line = drawLine("line" + coordLen, startPoint, endPoint);
			getId("intimeP").style.left = coordXY[0] - 15 + "px";
			getId("intimeP").style.top = coordXY[1] - 20 + "px";
			stepLen = 0;
		} else {
			stepLen++;
			if ((parseInt(endPoint.screenX) - parseInt(startPoint.screenX)) == 0) {
				toX = parseInt(endPoint.screenX);
				if (parseInt(endPoint.screenY) > parseInt(startPoint.screenY)) {
					toY = parseInt(startPoint.screenY) + stepNum * stepLen;
					if (toY > parseInt(endPoint.screenY)) {
						toY = parseInt(endPoint.screenY);
					}
				} else {
					toY = parseInt(startPoint.screenY) - stepNum * stepLen;
					if (toY < parseInt(endPoint.screenY)) {
						toY = parseInt(endPoint.screenY);
					}
				}
			} else if ((parseInt(endPoint.screenY) - parseInt(startPoint.screenY)) == 0) {
				toY = parseInt(endPoint.screenY);
				if (parseInt(endPoint.screenX) > parseInt(startPoint.screenX)) {
					toX = parseInt(startPoint.screenX) + stepNum * stepLen;
					if (toX > parseInt(endPoint.screenX)) {
						toX = parseInt(endPoint.screenX);
					}
				} else {
					toX = parseInt(startPoint.screenX) - stepNum * stepLen;
					if (toX < parseInt(endPoint.screenX)) {
						toX = parseInt(endPoint.screenX);
					}
				}
			} else {
				var tanA = Math
						.abs((parseInt(endPoint.screenY) - parseInt(startPoint.screenY)))
						/ Math
								.abs((parseInt(endPoint.screenX) - parseInt(startPoint.screenX)));
				if (parseInt(tanA) < 1) {
					if (parseInt(endPoint.screenX) > parseInt(startPoint.screenX)) {
						toX = parseInt(startPoint.screenX) + stepNum * stepLen;
						if (toX > parseInt(endPoint.screenX)) {
							toX = parseInt(endPoint.screenX);
						}
					} else {
						toX = parseInt(startPoint.screenX) - stepNum * stepLen;
						if (toX < parseInt(endPoint.screenX)) {
							toX = parseInt(endPoint.screenX);
						}
					}
					if (parseInt(endPoint.screenY) > parseInt(startPoint.screenY)) {
						toY = Math.abs(Math.abs(tanA
								* (toX - parseInt(startPoint.screenX)))
								+ parseInt(startPoint.screenY));
						if (toY > parseInt(endPoint.screenY)) {
							toY = parseInt(endPoint.screenY);
						}
					} else {
						toY = Math.abs(Math.abs(tanA
								* (toX - parseInt(startPoint.screenX)))
								- parseInt(startPoint.screenY));
						if (toY < parseInt(endPoint.screenY)) {
							toY = parseInt(endPoint.screenY);
						}
					}
				} else {
					tanA = Math
							.abs((parseInt(endPoint.screenX) - parseInt(startPoint.screenX)))
							/ Math
									.abs((parseInt(endPoint.screenY) - parseInt(startPoint.screenY)));
					if (parseInt(endPoint.screenY) > parseInt(startPoint.screenY)) {
						toY = parseInt(startPoint.screenY) + stepNum * stepLen;
						if (toY > parseInt(endPoint.screenY)) {
							toY = parseInt(endPoint.screenY);
						}
					} else {
						toY = parseInt(startPoint.screenY) - stepNum * stepLen;
						if (toY < parseInt(endPoint.screenY)) {
							toY = parseInt(endPoint.screenY);
						}
					}
					if (parseInt(endPoint.screenX) > parseInt(startPoint.screenX)) {
						toX = Math.abs(Math.abs(tanA
								* (toY - parseInt(startPoint.screenY)))
								+ parseInt(startPoint.screenX));
						if (toX > parseInt(endPoint.screenX)) {
							toX = parseInt(endPoint.screenX);
						}
					} else {
						toX = Math.abs(Math.abs(tanA
								* (toY - parseInt(startPoint.screenY)))
								- parseInt(startPoint.screenX));
						if (toX < parseInt(endPoint.screenX)) {
							toX = parseInt(endPoint.screenX);
						}
					}
				}
			}
			getId("empImg").style.left = toX + "px";
			getId("empImg").style.top = toY + "px";
			line.to = toX + "," + toY;
		}
		timeoutProcess = setTimeout(tarckLine, speed);
	}
}
function drawLine(id, startPoint, endPoint) {
	var s = "<v:line   "
			+ "id="
			+ id
			+ "   from="
			+ "'"
			+ (parseInt(startPoint.screenX))
			+ ","
			+ (parseInt(startPoint.screenY))
			+ "'"
			+ "   to="
			+ "'"
			+ (parseInt(endPoint.screenX))
			+ ","
			+ (parseInt(endPoint.screenY))
			+ "'"
			+ "  style='position:absolute;left:0px;top:0px;z-index:1000' StrokeColor=\"red\" strokeweight = \"3px\"></v:line>";
	var strStroke = "<v:stroke EndArrow=\"Classic\"></v:stroke>"
	var newStroke = document.createElement(strStroke);
	var o = document.createElement(s);
	o.insertBefore(newStroke);
	getId("mapDiv").appendChild(o);
	return o;
}
function createEmp() {
	var parentDiv = getId("mapDiv");
	var currentDiv = document.createElement("IMG");
	currentDiv.setAttribute("id", "empImg");
	currentDiv.setAttribute("src", "/SCMDB/images/emp.gif");
	currentDiv.style.position = "absolute";
	currentDiv.style.margin = "0px";
	currentDiv.style.height = "40px";
	currentDiv.style.width = "40px";
	currentDiv.style.padding = "0px";
	currentDiv.style.left = "0px";
	currentDiv.style.top = "0px";
	currentDiv.style.zIndex = "300";
	parentDiv.appendChild(currentDiv);

	currentDiv = document.createElement("P");
	currentDiv.setAttribute("id", "intimeP");
	currentDiv.style.position = "absolute";
	currentDiv.style.margin = "0px";
	currentDiv.style.height = "15px";
	currentDiv.style.width = "50px";
	currentDiv.style.background = "#e0e0e0";
	currentDiv.style.border = "1px solid #000";
	currentDiv.style.padding = "0px";
	currentDiv.style.left = "0px";
	currentDiv.style.top = "0px";
	currentDiv.style.zIndex = "300";
	currentDiv.style.visibility = "hidden";
	parentDiv.appendChild(currentDiv);
}